import { db } from '../index';
import { productCategories, caseCategories, tags } from '../schema';
import { createId } from '@paralleldrive/cuid2';
import { eq } from 'drizzle-orm';

/**
 * 分类种子数据
 */
export async function runCategorySeeds() {
  try {
    console.log('🔄 创建分类种子数据...');
    
    // 产品分类数据（根据产品分类.md文档）
    const productCategoryData = [
      {
        id: createId(),
        name: '品牌视觉设计',
        slug: 'brand-visual-design',
        description: 'LOGO与VI、名片、企业画册、品牌周边等品牌形象设计服务',
        image: 'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Brand%20visual%20design%20logo%20corporate%20identity%20modern%20professional&image_size=landscape_4_3',
        sort: 1,
        status: 'active'
      },
      {
        id: createId(),
        name: '宣传印刷',
        slug: 'promotional-printing',
        description: '宣传单页、海报、折页、不干胶标签、横幅旗帜等印刷品制作',
        image: 'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Promotional%20printing%20brochures%20flyers%20posters%20marketing%20materials&image_size=landscape_4_3',
        sort: 2,
        status: 'active'
      },
      {
        id: createId(),
        name: '包装制品',
        slug: 'packaging-products',
        description: '精品彩盒、手提袋、菜单台卡、瓶贴封口贴等包装产品',
        image: 'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Packaging%20products%20gift%20boxes%20shopping%20bags%20labels&image_size=landscape_4_3',
        sort: 3,
        status: 'active'
      },
      {
        id: createId(),
        name: '标识导视',
        description: '门头招牌、形象立招、腐蚀雕刻、铭牌奖牌等标识制作',
        sort: 4,
        status: 'active'
      },
      {
        id: createId(),
        name: '户外广告',
        description: '高炮大牌、灯箱拉布、公交站台、户外灯布等户外广告制作',
        sort: 5,
        status: 'active'
      },
      {
        id: createId(),
        name: '展览展示',
        description: '便携展具、展台展柜、背景文化墙、活动美陈等展示用品',
        sort: 6,
        status: 'active'
      },
      {
        id: createId(),
        name: '数字互动',
        description: 'LED大屏、触摸屏、全息投影等数字化展示设备',
        sort: 7,
        status: 'active'
      },
      {
        id: createId(),
        name: '车体广告',
        description: '车身贴、货车喷漆、车尾广告等车辆广告制作',
        sort: 8,
        status: 'active'
      },
      {
        id: createId(),
        name: '党建文化',
        description: '党建文化墙、党员先锋岗、党建雕塑等党建主题制作',
        sort: 9,
        status: 'active'
      },
      {
        id: createId(),
        name: '橱窗美陈',
        description: '橱窗贴膜、霓虹灯带、立体字等橱窗装饰',
        sort: 10,
        status: 'active'
      },
      {
        id: createId(),
        name: '光电标识',
        description: 'UV招牌灯箱、亚克力制品、铝边发光字等发光标识',
        sort: 11,
        status: 'active'
      },
      {
        id: createId(),
        name: '建筑装饰',
        description: '铝塑板门头、扣板门头、招牌灯带、精神堡垒等建筑装饰',
        sort: 12,
        status: 'active'
      },
      {
        id: createId(),
        name: '背胶应用',
        description: '玻璃贴、车身背胶、地贴墙贴、冰箱贴等背胶产品',
        sort: 13,
        status: 'active'
      }
    ];
    
    // 案例分类数据（根据案例分类.md文档）
    const caseCategoryData = [
      {
        id: createId(),
        name: '餐饮行业',
        slug: 'restaurant-industry',
        description: '餐厅、酒店、咖啡厅、快餐店等餐饮业广告设计案例',
        image: 'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Restaurant%20chain%20branding%20signage%20food%20industry%20modern%20design&image_size=landscape_4_3',
        sort: 1,
        status: 'active'
      },
      {
        id: createId(),
        name: '零售行业',
        slug: 'retail-industry',
        description: '商场、超市、专卖店、便利店等零售业广告案例',
        image: 'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Retail%20store%20signage%20shopping%20mall%20commercial%20design&image_size=landscape_4_3',
        sort: 2,
        status: 'active'
      },
      {
        id: createId(),
        name: '教育培训',
        description: '学校、培训机构、幼儿园等教育行业广告设计案例',
        sort: 3,
        status: 'active'
      },
      {
        id: createId(),
        name: '医疗健康',
        description: '医院、诊所、药店、美容院等医疗健康行业案例',
        sort: 4,
        status: 'active'
      },
      {
        id: createId(),
        name: '金融服务',
        description: '银行、保险、证券、投资等金融服务业广告案例',
        sort: 5,
        status: 'active'
      },
      {
        id: createId(),
        name: '房地产',
        description: '房地产开发、中介、装修等房地产行业广告案例',
        sort: 6,
        status: 'active'
      },
      {
        id: createId(),
        name: '汽车服务',
        description: '汽车销售、维修、美容、租赁等汽车服务业案例',
        sort: 7,
        status: 'active'
      },
      {
        id: createId(),
        name: '制造业',
        description: '工厂、生产企业、加工厂等制造业广告设计案例',
        sort: 8,
        status: 'active'
      },
      {
        id: createId(),
        name: '政府机构',
        description: '政府部门、事业单位、公共服务等政府机构案例',
        sort: 9,
        status: 'active'
      },
      {
        id: createId(),
        name: '文化娱乐',
        description: 'KTV、影院、游戏厅、健身房等文化娱乐业案例',
        sort: 10,
        status: 'active'
      }
    ];
    
    // 标签数据
    const tagData = [
      { id: createId(), name: '发光字', slug: 'led-letters', description: 'LED发光字制作', type: 'product', color: '#FF6B6B', sort: 1, status: 'active' },
      { id: createId(), name: 'LED显示屏', slug: 'led-display', description: 'LED显示屏安装', type: 'product', color: '#4ECDC4', sort: 2, status: 'active' },
      { id: createId(), name: '门头招牌', slug: 'storefront-sign', description: '门头招牌制作', type: 'product', color: '#45B7D1', sort: 3, status: 'active' },
      { id: createId(), name: '户外广告', slug: 'outdoor-advertising', description: '户外广告制作', type: 'product', color: '#96CEB4', sort: 4, status: 'active' },
      { id: createId(), name: '品牌设计', slug: 'brand-design', description: '品牌视觉设计', type: 'product', color: '#FFEAA7', sort: 5, status: 'active' },
      { id: createId(), name: '数字标牌', type: 'product', color: '#DDA0DD' },
      { id: createId(), name: '亚克力制品', type: 'product', color: '#98D8C8' },
      { id: createId(), name: '不锈钢字', type: 'product', color: '#F7DC6F' },
      { id: createId(), name: '霓虹灯', type: 'product', color: '#BB8FCE' },
      { id: createId(), name: '灯箱广告', type: 'product', color: '#85C1E9' },
      { id: createId(), name: '连锁品牌', type: 'case', color: '#FF9FF3' },
      { id: createId(), name: '大型项目', type: 'case', color: '#54A0FF' },
      { id: createId(), name: '创意设计', type: 'case', color: '#5F27CD' },
      { id: createId(), name: '技术创新', type: 'case', color: '#00D2D3' },
      { id: createId(), name: '环保材料', type: 'case', color: '#FF9F43' }
    ];
    
    // 插入产品分类
    for (const category of productCategoryData) {
      const existing = await db.select().from(productCategories).where(eq(productCategories.name, category.name)).limit(1);
      if (existing.length === 0) {
        await db.insert(productCategories).values(category);
        console.log(`✅ 创建产品分类: ${category.name}`);
      } else {
        console.log(`ℹ️ 产品分类 ${category.name} 已存在，跳过创建`);
      }
    }
    
    // 插入案例分类
    for (const category of caseCategoryData) {
      const existing = await db.select().from(caseCategories).where(eq(caseCategories.name, category.name)).limit(1);
      if (existing.length === 0) {
        await db.insert(caseCategories).values(category);
        console.log(`✅ 创建案例分类: ${category.name}`);
      } else {
        console.log(`ℹ️ 案例分类 ${category.name} 已存在，跳过创建`);
      }
    }
    
    // 插入标签
    for (const tag of tagData) {
      const existing = await db.select().from(tags).where(eq(tags.name, tag.name)).limit(1);
      if (existing.length === 0) {
        await db.insert(tags).values(tag);
        console.log(`✅ 创建标签: ${tag.name}`);
      } else {
        console.log(`ℹ️ 标签 ${tag.name} 已存在，跳过创建`);
      }
    }
    
  } catch (error) {
    console.error('❌ 分类种子数据创建失败:', error);
    throw error;
  }
}